#include <iostream>
#include <cerrno>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "comm.hpp"
using namespace std;
int main(){
    umask(0);//不影响OS，只会影响当前进程
    int n = mkfifo(fifoname.c_str(),mode);
    if(n!=0){
        cout<<errno<<":"<<strerror(errno)<<endl;
        return 1;
    }
    cout<<"create fifo file success"<<endl;
    //2.让服务端开启管道文件
    int rfd = open(fifoname.c_str(),O_RDONLY);
    if(rfd<0){
        cout<<errno<<":"<<strerror(errno)<<endl;
        return 2;
    }
    cout<<"opend fifo success,begin ipc"<<endl;

    //3.正常通信
    char buffer[NUM];
    while(true){
        buffer[0]=0;
        ssize_t n = read(rfd,buffer,sizeof(buffer)-1);
        if(n>0){
            //buffer[n]=0;
            printf("%s",buffer);
            fflush(stdout);
        }else if(n==0){
            cout<<"client quit me too"<<endl;
            break;
        }else{
            cout<<errno<<":"<<strerror(errno)<<endl;
            break;
        }
    }
    close(rfd);
    unlink(fifoname.c_str());
    return 0;
}